﻿/*
 * Copyright (c) 2010, Sergey Loktin (mailto://shadowconsp@gmail.com)
 * Licensed under The MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using MoonyPlugin;
using System.Runtime.Serialization;
using System.Configuration;

namespace MoonyDesk
{
    /// <summary>
    /// Interaction logic for SettingsWindow.xaml
    /// MoonyDesk settings window
    /// </summary>
    public partial class SettingsWindow : Window
    {
        public SettingsWindow()
        {
            InitializeComponent();
            try
            {
                //add tab for each plugin in settings window
                foreach (LoadedPlugin plugin in PluginSystem.LoadedPlugins)
                {
                    TabItem tabItem = new TabItem();
                    CheckBox cb = new CheckBox();
                    cb.IsChecked = plugin.Active;
                    cb.Checked += new RoutedEventHandler(cb_Checked);
                    cb.Unchecked += new RoutedEventHandler(cb_Unchecked);
                    cb.Tag = plugin;
                    TextBlock tb = new TextBlock();
                    tb.Text = plugin.Name;
                    StackPanel stackPanel = new StackPanel();
                    stackPanel.Orientation = Orientation.Horizontal;
                    stackPanel.Children.Add(cb);
                    stackPanel.Children.Add(tb);
                    tabItem.Header = stackPanel;
                    if(plugin.Active)
                        tabItem.Content = new PluginSettingsCtrl(plugin);
                    tabControl.Items.Add(tabItem);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Settings initialize error:\n" + ex.ToString());
            }
        }

        /// <summary>
        /// activate plugin
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void cb_Checked(object sender, RoutedEventArgs e)
        {
            CheckBox cb = (sender as CheckBox);
            LoadedPlugin plugin = (cb.Tag as LoadedPlugin);
            DependencyObject parent = VisualTreeHelper.GetParent(cb);
            while (parent.GetType() != typeof(TabItem))
                parent = VisualTreeHelper.GetParent(parent);
            (parent as TabItem).IsSelected = true;

            (cb.Tag as LoadedPlugin).Activate();
            (parent as TabItem).Content = new PluginSettingsCtrl(plugin);
            (parent as TabItem).InvalidateVisual();
        }

        /// <summary>
        /// diactivate plugin
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void cb_Unchecked(object sender, RoutedEventArgs e)
        {
            CheckBox cb = (sender as CheckBox);
            DependencyObject parent = VisualTreeHelper.GetParent(cb);
            while (parent.GetType() != typeof(TabItem))
                parent = VisualTreeHelper.GetParent(parent);

            (parent as TabItem).Content = null;
            (cb.Tag as LoadedPlugin).Diactivate();
            (parent as TabItem).InvalidateVisual();
        }

        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            //save settings for each plugin on settings window closing
            foreach (LoadedPlugin plugin in PluginSystem.LoadedPlugins)
            {
                SaveSettings(plugin);
            }
        }

        private void SaveSettings(LoadedPlugin plugin)
        {
            Properties.Settings.Default.actives[plugin.Name] = plugin.Active.ToString();
            if (plugin.Active)
            {
                Properties.Settings.Default.xPositions[plugin.Name] = plugin.MainWindow.Left.ToString();
                Properties.Settings.Default.yPositions[plugin.Name] = plugin.MainWindow.Top.ToString();
                Properties.Settings.Default.movables[plugin.Name] = plugin.MainWindow.Movable.ToString();
                Properties.Settings.Default.topmosts[plugin.Name] = plugin.MainWindow.Topmost2.ToString();
                Properties.Settings.Default.sizes[plugin.Name] = plugin.MainWindow.viewBox.Width.ToString();
                Properties.Settings.Default.angles[plugin.Name] = plugin.MainWindow.rotate.Angle.ToString();
            }
            Properties.Settings.Default.Save();
        }
    }
}
